package cn.kgc.spring.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.weaver.ast.Var;
import org.springframework.stereotype.Component;

/**
 * @author: mosin
 * @version: v1.0  2022/10/13
 * 通知类
 * 定义多个通知
 */
@Component
@Aspect
public class MyAdvice {

    // 定义公共的切点
    @Pointcut("execution(* cn.kgc.spring.service.UserServiceImpl.*(..))")
    public void pointCut(){}

    //前置通知
    @Before("pointCut()")
    public void before(){
        System.out.println("before前置通知");
    }


    // 环绕通知
    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint){

        try {
            System.out.println("----------around前置通知------------");
            // 标识执行核心业务代码
            Object proceed = joinPoint.proceed();
            System.out.println("----------around后置通知------------");
            return proceed;
        } catch (Throwable throwable) {
            System.out.println("----------around异常通知------------");
            throwable.printStackTrace();
        }finally {
            System.out.println("----------around最终通知------------");
        }
        return null;
    }

}
